<template>
  <div class="search-wrapper">
    <el-row type="flex" align="middle" class="search-bar">
      <input
        placeholder="请输入想去的地方，比如：'广州'"
        v-model="userKey"
        @keyup.enter="onSearch(userKey)"
      />
      <i class="el-icon-search" @click="onSearch(userKey)"></i>
    </el-row>
    <div class="search-recommend">
      推荐：
      <span
        v-for="(recommendCity, index) in recommendCities"
        :key="index"
        @click="onSearch(recommendCity)"
        >{{ recommendCity }}</span
      >
    </div>
  </div>
</template>

<script>
import bus from "./utils/postBus";
import { postList } from "@/utils/apis/post";
export default {
  data() {
    return {
      userKey: "",
      userCity: "",
      recommendCities: ["广州", "上海", "北京"],
    };
  },

  methods: {
    async onSearch(userKey) {
      this.userKey = userKey;
      let city = userKey;
      if (city === "") {
        city = undefined;
      }
      let res = await postList({ city });
      bus.$emit("postList", res);
      // 点击搜索的时候通过地址栏把搜索状态显示给用户
      this.$router.push({
        path: "/post",
        query: userKey !== "" ? { city: userKey } : undefined,
      });
    },
  },
};
</script>

<style lang="less" scoped>
.search-wrapper {
  .search-bar {
    width: 100%;
    box-sizing: border-box;
    height: 40px;
    line-height: 40px;
    border: 3px solid orange;

    input {
      flex: 1;
      padding: 0 20px;
      line-height: 40px;
      outline: none;
      border: none;
      background: none;
    }

    .el-icon-search {
      font-size: 24px;
      color: orange;
      font-weight: 700;
      margin-right: 10px;
      cursor: pointer;
    }
  }
  .search-recommend {
    padding: 10px 0;
    font-size: 12px;
    color: #666;
    span {
      margin-right: 5px;
      cursor: pointer;
      &:hover {
        color: orange;
      }
    }
  }
}
</style>
